package com.ruoyi.ly.service.impl;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.ly.domain.Project;
import com.ruoyi.ly.domain.SpaceBan;
import com.ruoyi.ly.domain.SpaceSite;
import com.ruoyi.ly.domain.SpaceSiteGroup;
import com.ruoyi.ly.domain.dto.PageWorkSpaceDTO;
import com.ruoyi.ly.domain.dto.SpaceBanAddDTO;
import com.ruoyi.ly.domain.dto.WorkSpaceAddDTO;
import com.ruoyi.ly.domain.dto.WorkSpaceUpdateDTO;
import com.ruoyi.ly.domain.vo.*;
import com.ruoyi.ly.mapper.SpaceBanMapper;
import com.ruoyi.ly.mapper.SpaceSiteMapper;
import com.ruoyi.ly.mapper.WorkSpaceMapper;
import com.ruoyi.ly.service.WorkSpaceService;
import com.ruoyi.ly.utils.PageResult;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private WorkSpaceMapper workSpaceMapper;
    @Autowired
    private SpaceSiteMapper spaceSiteMapper;
    @Autowired
    private SpaceBanMapper spaceBanMapper;


    //分页查询
    @Override
    public PageResult pageQuery(PageWorkSpaceDTO pageWorkSpaceDTO) {
        PageHelper.startPage(pageWorkSpaceDTO.getPage(),pageWorkSpaceDTO.getPageSize());
        Page<PageWorkSpaceVO> page = workSpaceMapper.pageQuery(pageWorkSpaceDTO);
        long total = page.getTotal();
        List<PageWorkSpaceVO> records = page.getResult();
        return new PageResult(total,records);
    }

    //工作区删除
    @Override
    public void delete(Long siteGroupId) {

        workSpaceMapper.delete(siteGroupId);
        //工作区删除完成之后要删除工作区对应的工位
        spaceSiteMapper.delete(siteGroupId);
    }

    //创建工作区
    @Override
    public void insert(WorkSpaceAddDTO workSpaceAddDTO) {



        List<String> siteNameList = workSpaceAddDTO.getSiteNameList();
        //对工作区表进行操作
        SpaceSiteGroup spaceSiteGroup = new SpaceSiteGroup();
        BeanUtils.copyProperties(workSpaceAddDTO,spaceSiteGroup);
        //工位数量
        spaceSiteGroup.setSiteNum(siteNameList.size());
        spaceSiteGroup.setCtime(String.valueOf(LocalDateTime.now()));//TODO 数据库字段设计有问题  有几个字段不能插入
        workSpaceMapper.insert(spaceSiteGroup);

        //workSpaceAddDTO里面没有SiteGroupId,FirmId,Status 用spaceSiteGroup来进行赋值

        for (String siteName : siteNameList) {
            //对工位表进行操作
            SpaceSite spaceSite = new SpaceSite();
            spaceSite.setSiteName(siteName);
            spaceSite.setSiteGroupId(spaceSiteGroup.getSiteGroupId());
            spaceSite.setFirmId(1L);//  TODO FirmId 不能为空 但是spaceSiteGroup里面的FirmId为空 我只是在xml里面进行了赋值
            spaceSite.setStatus(1L);//  TODO 同上需要解决
            spaceSiteMapper.insert(spaceSite);
        }
    }

    //根据工作区siteGroupId查询楼栋
    @Override
    public WorkSpaceDetailVO selectById(Long siteGroupId) {

        WorkSpaceDetailVO workSpaceDetailVO = workSpaceMapper.selectById(siteGroupId);
        //对siteNameList字段进行赋值
          List<String> siteNameList = spaceSiteMapper.selectBySiteGroupId(siteGroupId);
          workSpaceDetailVO.setSiteNameList(siteNameList);
      //  List<T> list = JSON.parseArray(workSpaceDetailVO.getImgs().toString(),T.class);
      //  workSpaceDetailVO.setImgs(list);

        return workSpaceDetailVO;
    }

    //修改工作区
    @Override
    public void update(WorkSpaceUpdateDTO workSpaceUpdateDTO) {
        SpaceSiteGroup spaceSiteGroup = new SpaceSiteGroup();
        BeanUtils.copyProperties(workSpaceUpdateDTO, spaceSiteGroup);
        workSpaceMapper.update(spaceSiteGroup);
        workSpaceUpdateDTO.setSiteGroupId(spaceSiteGroup.getSiteGroupId());

        Long siteGroupId = spaceSiteGroup.getSiteGroupId();
        //先删除工位 然后在进行插入
        spaceSiteMapper.delete(siteGroupId);


        SpaceSite spaceSite = new SpaceSite();
        spaceSite.setSiteGroupId(siteGroupId);
        spaceSite.setStatus(1L);//TODO  status默认赋值
        spaceSite.setFirmId(1L);//TODO  firmID默认赋值
        //对siteNameList字段进行赋值
        workSpaceUpdateDTO.getSiteNameList().forEach(item->{
            spaceSite.setSiteName(item);
            spaceSiteMapper.insert(spaceSite);
        });


    }

    //根据项目id查询所属楼栋
    @Override
    public List<WorkSpaceAddBanVO> selectByProjectId(Long projectId) {
        //根据项目id查询所属楼栋
        List<WorkSpaceAddBanVO> workSpaceAddBanVOList = spaceBanMapper.slectByProjectId(projectId);
        return workSpaceAddBanVOList;
    }


    //根据项目id和楼栋id查询所属楼层
    @Override
    public List<WorkSpaceAddFloorVO> selectByProjectAndBanId(@Param("projectId")Long projectId,@Param("banId") Long banId) {
        List<WorkSpaceAddFloorVO> workSpaceAddFloorVOList =spaceBanMapper.selectByProjectAndBanId(projectId,banId);
        return workSpaceAddFloorVOList;
    }

    //查询所有的楼栋
    @Override
    public List<SpaceBan> selectAllBan() {
        List<SpaceBan> spaceBanList = spaceBanMapper.selectAllBan();
        return spaceBanList;
    }

    //查询所有的项目
    @Override
    public List<Project> selectAllProject() {
       List<Project> projectList = spaceBanMapper.selectAllProject();
        return projectList;
    }


}
